package com.roger.myHome.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roger.myHome.domain.TbMenus;
import com.roger.myHome.entity.Menu;
import com.roger.myHome.entity.MenuReq;
import com.roger.myHome.mapper.MenusMapper;
import com.roger.myHome.service.MenuService;
import com.roger.myHome.service.UrlService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenusMapper menusMapper;

    @Resource
    private UrlService urlService;

    @Override
    public List<Menu> selectMenus(MenuReq menuReq) {
        QueryWrapper<TbMenus> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","menu_name","icon","url").eq("parent_id",menuReq.getParentId()).orderByAsc("serial_number");
        List<TbMenus> list = menusMapper.selectList(queryWrapper);
        List<Menu> menuList = new ArrayList<>();
        for (TbMenus tbMenus : list) {
            Menu menu = new Menu();
            menu.setId(tbMenus.getId());
            menu.setParentId(tbMenus.getParentId());
            menu.setIcon(tbMenus.getIcon());
            menu.setMenuName(tbMenus.getMenuName());
            menu.setUrl(tbMenus.getUrl());
            menuList.add(menu);
        }
        return menuList;
    }

    @Override
    @Cacheable(value = "urls",keyGenerator  = "myKeyGenerator")
    public List<String> selectNotNull() {
        System.out.println("==query url list from db");
        return urlService.list();
    }
}
